import 'package:flutter/material.dart';

/// Dart类型系统示例
class TypesExample extends StatefulWidget {
  const TypesExample({super.key});

  @override
  State<TypesExample> createState() => _TypesExampleState();
}

class _TypesExampleState extends State<TypesExample> {
  String result = '点击下方按钮查看不同类型的使用示例';

  void _demonstrateBasicTypes() {
    // 基本类型
    int age = 25;
    double height = 1.75;
    String name = 'Flutter';
    bool isActive = true;

    setState(() {
      result = '''
【基本类型演示】
int: $age
double: $height
String: $name
bool: $isActive
      ''';
    });
  }

  void _demonstrateCollections() {
    // 集合类型
    List<int> numbers = [1, 2, 3, 4, 5];
    Set<String> uniqueNames = {'Alice', 'Bob', 'Charlie'};
    Map<String, int> scores = {'数学': 95, '语文': 88, '英语': 92};

    setState(() {
      result = '''
【集合类型演示】
List: $numbers
  - 第一个元素: ${numbers.first}
  - 最后一个元素: ${numbers.last}
  - 长度: ${numbers.length}

Set: $uniqueNames
  - 包含Alice: ${uniqueNames.contains('Alice')}

Map: $scores
  - 数学成绩: ${scores['数学']}
  - 所有科目: ${scores.keys.toList()}
      ''';
    });
  }

  void _demonstrateTypeInference() {
    // 类型推断
    var inferredInt = 42; // Dart自动推断为int
    var inferredString = 'Hello'; // 推断为String
    var inferredList = [1, 2, 3]; // 推断为List<int>

    setState(() {
      result = '''
【类型推断演示】
使用var关键字，Dart会自动推断类型：

var inferredInt = 42;
  → 类型: ${inferredInt.runtimeType}
  → 值: $inferredInt

var inferredString = 'Hello';
  → 类型: ${inferredString.runtimeType}
  → 值: $inferredString

var inferredList = [1, 2, 3];
  → 类型: ${inferredList.runtimeType}
  → 值: $inferredList
      ''';
    });
  }

  void _demonstrateSpecialTypes() {
    // 特殊类型
    dynamic dynamicVar = 'Can be anything';
    dynamicVar = 42; // 可以改变类型

    Object objectVar = 'Also flexible';
    
    num numVar = 3.14; // num可以是int或double
    numVar = 42;

    setState(() {
      result = '''
【特殊类型演示】
dynamic - 可以是任何类型:
  → 当前值: $dynamicVar (${dynamicVar.runtimeType})

Object - 所有类型的基类:
  → 值: $objectVar

num - int和double的父类:
  → 值: $numVar (${numVar.runtimeType})
      ''';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Dart 类型系统'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // 说明卡片
            Card(
              color: Colors.blue.shade50,
              child: const Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '📚 类型系统概述',
                      style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 8),
                    Text(
                      'Dart是强类型语言，支持类型推断。主要类型包括：\n'
                      '• 基本类型：int, double, String, bool\n'
                      '• 集合类型：List, Set, Map\n'
                      '• 特殊类型：dynamic, Object, num',
                      style: TextStyle(fontSize: 14),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),

            // 代码示例区域
            Container(
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: Colors.grey.shade200,
                borderRadius: BorderRadius.circular(8),
              ),
              child: const Text(
                '''// 基本类型
int age = 25;
double height = 1.75;
String name = 'Flutter';
bool isActive = true;

// 集合类型
List<int> numbers = [1, 2, 3];
Set<String> names = {'Alice', 'Bob'};
Map<String, int> scores = {'math': 95};

// 类型推断
var autoInt = 42; // 推断为int
var autoList = [1, 2, 3]; // 推断为List<int>

// 特殊类型
dynamic flexible = 'any type';
num number = 3.14; // int或double''',
                style: TextStyle(
                  fontFamily: 'monospace',
                  fontSize: 13,
                ),
              ),
            ),
            const SizedBox(height: 16),

            // 交互按钮
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                ElevatedButton.icon(
                  onPressed: _demonstrateBasicTypes,
                  icon: const Icon(Icons.format_list_numbered),
                  label: const Text('基本类型'),
                ),
                ElevatedButton.icon(
                  onPressed: _demonstrateCollections,
                  icon: const Icon(Icons.collections),
                  label: const Text('集合类型'),
                ),
                ElevatedButton.icon(
                  onPressed: _demonstrateTypeInference,
                  icon: const Icon(Icons.auto_awesome),
                  label: const Text('类型推断'),
                ),
                ElevatedButton.icon(
                  onPressed: _demonstrateSpecialTypes,
                  icon: const Icon(Icons.star),
                  label: const Text('特殊类型'),
                ),
              ],
            ),
            const SizedBox(height: 16),

            // 结果展示区域
            Card(
              elevation: 4,
              child: Container(
                width: double.infinity,
                padding: const EdgeInsets.all(16),
                constraints: const BoxConstraints(minHeight: 150),
                child: Text(
                  result,
                  style: const TextStyle(
                    fontSize: 14,
                    fontFamily: 'monospace',
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

